<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt"%>
<%@ taglib uri="/WEB-INF/tlds/oscache.tld" prefix="oscache"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.json.*"%>
<%@ page import="com.gzgi.framework.*"%>
<%@ page import="com.gzgi.framework.el.*"%>
<%@ page import="com.gzgi.framework.util.*"%>
<%@ page import="com.gzgi.framework.config.*"%>
<%@ page import="com.gzgi.framework.context.*"%>
<%@ page import="com.gzgi.framework.tree.*"%>
<%@ page import="com.gzgi.framework.dataitem.*"%>
<%@ page import="com.gzgi.framework.sys.*"%>
<%@ page import="com.gzgi.framework.todo.*"%>
<%@ page import="com.gzgi.framework.calendar.*"%>
<%@ page import="com.gzgi.framework.sys.model.*"%>
<%@ page import="com.gzgi.workflow.jbpm.model.*"%>
<%@ page import="com.gzgi.workflow.jbpm.container.*"%>
<%@ page import="org.apache.commons.lang.StringUtils"%>
<%
    String context = request.getContextPath();
	TodoService todoService = (TodoService)ContextFactory.getBean("todoService");
	SqlMapManager sqlMapManager = (SqlMapManager)ContextFactory.getBean("sqlMapManager");
	WorkCalendarService workCalendarService =  (WorkCalendarService) ContextFactory.getBean("workCalendarService");
	Map params = new HashMap();
	long now = System.currentTimeMillis();
	String actorId = request.getParameter("actorId"); 
	Collection actorIds = new ArrayList();
	actorIds.add(actorId);
    params.put("actorIds", actorIds);
	List rows = sqlMapManager.getList("QUERY_TASK", params);
	List tasks = new ArrayList();
	List todos =  todoService.getTodoList();
	if(rows != null && rows.size() > 0 && todos != null){
		Iterator iterator = todos.iterator();
		while(iterator.hasNext()){
			Todo todo = (Todo)iterator.next();
			double limitDay = todo.getLimitDay();
			int ahour = todo.getXa();
			int bhour = todo.getXb();
			boolean hasTask = false;
            String processName = todo.getProcessName();
			String taskName = todo.getTaskName();
			Iterator iter = rows.iterator();
		    while(iter.hasNext()){
				 TaskItem item = (TaskItem)iter.next();
				 if(StringUtils.isNotEmpty(item.getRowId()) && 
					  StringUtils.equals(processName, item.getProcessName()) &&
					  StringUtils.equals(taskName, item.getTaskName())){
                      Date limitWorkDate = workCalendarService.getWorkDate(item.getCreateDate(), (int) limitDay);
					  long time = limitWorkDate.getTime();
					  Date cautionDate = new Date(time - ahour * DateTools.HOUR);
					  Date pastDueDate = new Date(time + bhour * DateTools.HOUR);
					  int status = 0;
					  if (pastDueDate != null && now > pastDueDate.getTime()) {
							status = TodoUtils.PAST_DUE_STATUS;
					  } else if (cautionDate != null && now < cautionDate.getTime()) {
							status = TodoUtils.OK_STATUS;
					  } else {
							status = TodoUtils.CAUTION_STATUS;
					  }
					  if ( item.getCreateDate() != null) {
							if (pastDueDate.getTime() < item.getCreateDate().getTime()
									|| cautionDate.getTime() < item.getCreateDate().getTime()) {
								status = TodoUtils.PAST_DUE_STATUS;
							}
					 }
					 TodoInstance x = new TodoInstance();
					 x.setTodo(todo);
					 x.setActorId(item.getActorId());
					 x.setRowId(item.getRowId());
					 todo.getAllBuffer().append(item.getRowId());
					 hasTask = true;
					 switch(status){
						  case TodoUtils.CAUTION_STATUS:
                              todo.getCaution().add(x);
						      todo.getCautionBuffer().append(item.getRowId());
							  break;
						  case TodoUtils.PAST_DUE_STATUS:
                              todo.getPastDue().add(x);
						      todo.getPastDueBuffer().append(item.getRowId());
							  break;
						  default:
							  todo.getOk().add(x);
						      todo.getOkBuffer().append(item.getRowId());
							  break;
					 }
				 }
			 }
			 if(hasTask){
				 tasks.add(todo);
			 }
		}
	}
%>
<link rel="stylesheet" type="text/css"
	href="<%=request.getContextPath()%>/pages/styles/styles.css" />
<style type="text/css">
.todo_title {
	position: absolute;
	top: 0;
	left: 0;
	background: url(../images/todo_title.gif) no-repeat;
	width: 55px;
	height: 15px;
}
</style>
<div class="todo">
<div align="left" width="80%">
<div align="left"></div>
</div>
<div>
<table width="80%" border="0" cellspacing="1" cellpadding="0"	class="list-box">
	<tr class="list-title">
		<td align="center"><b>事&nbsp;&nbsp;项</b></td>
		<td width="95" align="center"><b>PastDue</b></td>
		<td width="95" align="center"><b>Caution</b></td>
		<td width="95" align="center"><b>OK</b></td>
	</tr>
	<%if(tasks != null && tasks.size()> 0){
		      Map paramMap = new HashMap();
			  Iterator iterator008 = tasks.iterator();
			  while(iterator008.hasNext()){
	               Todo xx = (Todo) iterator008.next();
				   pageContext.setAttribute("todo", xx);
				   paramMap.clear();
				   paramMap.put("todoId", xx.getId());				 
				   int okQty = xx.getOk().size();
				   int cautionQty = xx.getCaution().size();
				   int pastDueQty = xx.getPastDue().size();
                   String x_complex_query = JSONTools.encode(paramMap);
                   x_complex_query = RequestUtil.encodeString(x_complex_query);
			  %>
	<tr class="list-a">
		<td height="20" align="left"><a
			href="<%=context%><c:out value="${todo.listLink}" escapeXml="true"/>&todoId=<c:out value="${todo.id}"/>&task=<c:out value="${todo.taskName}"/>&x_complex_query=<%=x_complex_query%>"
			title="<c:out value="${todo.title}"/>   <c:out value="${todo.content}"/>" target="_blank"> <c:out value="${todo.title}" /> </a></td>
		<td align="center" class="red"><%=pastDueQty%></td>
		<td align="center" class="yellow"><%=cautionQty%></td>
		<td align="center" class="green"><%=okQty%></td>
	</tr>
	<%     }
		  }
	%>
</table>
</div>
</div>